package ui.graphic;

import Modelos.*;
import ui.graphic.MenuCartas;
import ui.graphic.maps.MapDataModel;
import ui.graphic.maps.MapData;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.sound.sampled.LineUnavailableException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

/**
 *
 * @author gustavodinis
 */
public class Frame extends JFrame {

    ModeloTabuleiro mT;
    ModeloCartas mC;

    MapData data;
    Painel j;
    MenuCartas cartas;
    InfoLateral iLateral;
//     Controls controls;
    MapDataModel modelo;
    JMenuItem addMenu, limpaMenu;
    Font letra, letra2;
    JMenu sairMenu;
    Boolean end;

    Frame(String nome, int x, int y, int h, int w, MapData data, MapDataModel m, ModeloTabuleiro mX, ModeloCartas mY) throws LineUnavailableException {
        super(nome);

        mT = mX;
        mC = mY;

//        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
//        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
//        setBounds(0,0,screenSize.width, screenSize.height);
//        setPreferredSize(new Dimension(1280, 671));
//        setResizable(false);
//        setUndecorated(true);
        GraphicsDevice myDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        //myDevice.setFullScreenWindow(this);
        setBounds(0, 0, getToolkit().getScreenSize().width, getToolkit().getScreenSize().height);

//        setLocation(0, 0);
        this.data = data;
        this.modelo = m;
        try {
            j = new Painel(data, m, mT);

        } catch (IOException e) {
            System.out.println("Erro!");
        }

        iLateral = new InfoLateral(mT, mC);
        cartas = new MenuCartas(mC);
        m.addObserver(j);

//        setSize(h, w);
//        setLocation(x, y);
        JMenuBar barraMenu = new JMenuBar();
        JMenu imgMenu = new JMenu("Gravar");
        barraMenu.add(imgMenu);
        sairMenu = new JMenu("Sair");
        barraMenu.add(sairMenu);
//        limpaMenu = new JMenuItem("Limpa");
//        imgMenu.add(limpaMenu);
//        addMenu = new JMenuItem("Acrescenta");
//        imgMenu.add(addMenu);
//        imgMenu.addSeparator();

        setJMenuBar(barraMenu);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //////// FONTES //////////
        URL fontPath = getClass().getResource("resources/font/m.ttf");
        URL font2Path = getClass().getResource("resources/font/font.ttf");

        try {
            GraphicsEnvironment ge
                    = GraphicsEnvironment.getLocalGraphicsEnvironment();
            //MontereyMediumFLF
            letra = Font.createFont(Font.TRUETYPE_FONT, fontPath.openStream()).deriveFont(Font.PLAIN, 15);
            GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(letra);
            //Remachine Script Personal Use
            letra2 = Font.createFont(Font.TRUETYPE_FONT, font2Path.openStream()).deriveFont(Font.PLAIN, 15);

        } catch (IOException | FontFormatException e) {
            System.out.println("erro");
        }

        //Bet
        fazLayout();
        motionList();
    }

    final void fazLayout() {
        Container cp = getContentPane();
        cp.setBackground(Color.red);
        cp.setLayout(new BorderLayout());
        cp.add(j, BorderLayout.CENTER);
        //cp.add(lateral, BorderLayout.WEST);
//        lateral.setLayout( new GridLayout( 1, 6 ) );
        cp.add(iLateral, BorderLayout.WEST);
        cp.add(cartas, BorderLayout.EAST);
        cp.validate();
    }

    public void motionList() {

//        MouseAdapter desenha, cclean;
//        desenha = new MouseAdapter() {
//            @Override
//            public void mousePressed(MouseEvent e) {
//                //setBackground(Color.red);
////                modelo.addFigura();
//            }
//        };
//
//        addMenu.addMouseListener(desenha);

//        cclean = new MouseAdapter() {
//            @Override
//            public void mousePressed(MouseEvent e) {
//                System.exit(0);
//            }
//        };
//        sairMenu.addMouseListener(cclean);
    }

    public void andCardPopUp() {
        if (!mT.getActivePlayer().isIa()) {

            Object[] options = {"Acção 1",
                "Acção 2"};
            int n = JOptionPane.showOptionDialog(this,
                    "Deseja fazer a acção 1 ou 2?",
                    "Que acção realizar",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE,
                    null, //do not use a custom Icon
                    options, //the titles of buttons
                    options[0]); //default button title
            mT.chooseAction(n);
        }
    }

    public void orCardPopUp() {
        if (!mT.getActivePlayer().isIa()) {
            Object[] options = {"Acção 1",
                "Acção 2"};
            int n = JOptionPane.showOptionDialog(this,
                    "Deseja fazer a acção 1 ou 2?",
                    "Que acção realizar",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE,
                    null, //do not use a custom Icon
                    options, //the titles of buttons
                    options[0]); //default button title
            mT.chooseAction(n);
        }
    }

    public static boolean isInteger(String s) {
        try {
            Integer.parseInt(s);
        } catch (NumberFormatException e) {
            return false;
        }
        // only got here if we didn't return false
        return true;
    }
}
